package pack1;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class UserDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	public int save(UserDet userdet)
	{
		return (Integer) sessionFactory.getCurrentSession().save(userdet);
	}
	
	
	public int getSeq()
	{
		int res= 0;
		try{
		int actual_seq = (Integer) sessionFactory.getCurrentSession().createCriteria(UserDet.class)
				.setProjection(Projections.max("id")).uniqueResult();
		
		
		int seq= actual_seq % 10000;
		DateFormat dateFormat = new SimpleDateFormat("yyMM");
		String dbseq_val= dateFormat.format(Calendar.getInstance().getTime());
		res= (Integer.parseInt(dbseq_val)*10000)+(seq+1);
		return res;
		}catch(Exception e){
			System.out.println("Exception occurs");
			e.printStackTrace();
		}
		return res;
	}
	
	
}
